package org.jeecg.modules.purchase.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.lottery.service.ILotteryManagementService;
import org.jeecg.modules.purchase.Vo.PurchaseSchemeVo;
import org.jeecg.modules.purchase.entity.PurchasePlan;
import org.jeecg.modules.purchase.service.IPurchasePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(tags = "采购编制44")
@RestController
@RequestMapping("/procurement/purchasescheme")
@Slf4j
public class PurchaseSchemeController {

    @Autowired
    private IPurchasePlanService purchasePlanService;
    @Autowired
    private ILotteryManagementService lotteryManagementService;

    @ApiOperation(value = "采购计划-获取已审批计划", notes = "采购计划-获取已审批计划")
    @GetMapping(value = "/getApprovedPlan")
    public Result<?> getApprovedPlan(@RequestParam String id) {
        if (id == null || id.trim().isEmpty()) {
            return Result.error("采购计划ID不能为空");
        }
        PurchasePlan purchasePlan = purchasePlanService.getApprovedPlanById(id);
        if (purchasePlan == null) {
            return Result.error("未找到已审批的采购计划");
        }
        return Result.OK(purchasePlan);
    }

    //新添采购编制
    @AutoLog(value = "采购编制-编辑")
    @ApiOperation(value = "采购编制-编辑", notes = "采购编制-编辑")
    @PostMapping(value = "/edit")
    public Result<?> add(@RequestBody PurchaseSchemeVo purchaseSchemeVo) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        if (purchasePlanService.addPurchaseScheme(purchaseSchemeVo, sysUser.getUsername())) {
            return Result.OK("新添成功/草稿");
        }
        return Result.error("新添失败/草稿");
    }

    //提交审核
    @AutoLog(value = "采购编制-发布")
    @ApiOperation(value = "采购编制-发布", notes = "采购编制-发布")
    @PostMapping(value = "/submit")
    public Result<?> submit(@RequestParam(name = "id", required = true) String id) {
        // 校验ID是否为空（虽然@RequestParam(required=true)已保证，但增加安全校验）
        if (id == null || id.trim().isEmpty()) {
            return Result.error("采购计划ID不能为空");
        }

        // 获取当前登录用户
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 从数据库中获取采购方案
        PurchasePlan purchasePlan = purchasePlanService.getById(id);
        if (purchasePlan == null) {
            return Result.error("未找到对应数据");
        }

        // 更新状态为发布
        purchasePlan.setStatus("1");
        purchasePlan.setUpdateTime(new Date());
        purchasePlan.setUpdateBy(sysUser.getUsername());
        if (purchasePlanService.updateById(purchasePlan)) {
            lotteryManagementService.add(purchasePlan.getId(),purchasePlan.getCategory());
            return Result.OK("发布成功");
        }
        return Result.error("发布失败");
    }
}
